﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SiteRequireLogin.Master" AutoEventWireup="true" CodeBehind="ProductMgmt.aspx.cs" Inherits="TranTekBE.ProductMgmt" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="Scripts/jquery.browser.js" type="text/javascript"></script>
    <script src="Scripts/jquery.menu.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" runat="server">
    <asp:UpdatePanel ID="UpdatePanel" runat="server" onload="UpdatePanel_Load">
        <ContentTemplate>
            <h1>Products</h1>
            <input type="hidden" runat="server" id="hdList" />
            <table style="width:100%">
                <tr>
                    <td class="tdLeftM msgError"><asp:Literal ID="ltError" runat="server" /></td>
                </tr>
                <tr>
                    <td class="tdLeftM">
                        <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="70px" CommandName="Delete" OnClientClick="return DeleteConfirm()" oncommand="ControlClick_Command" /> <%--onclick="btnDelete_Click"--%>
                        <button id="btnAdd" style="width:70px;" type="button">Add</button>
                        <asp:Button ID="btnSort" runat="server" Text="Sort By" Width="70px" CommandName="Sort" oncommand="ControlClick_Command" />
                        <asp:DropDownList ID="ddlSortBy" runat="server">
                            <asp:ListItem Text="Product Name" Value="proName" Selected="True" />
                            <asp:ListItem Text="Onhand Qty" Value="proOnHandQty" />
                            <asp:ListItem Text="Active" Value="proStatus" />
                            <asp:ListItem Text="Price" Value="proPrice" />
                        </asp:DropDownList>
                        <asp:DropDownList ID="ddlSortDirect" runat="server">
                            <asp:ListItem Text="Ascending" Value="ASC" Selected="True" />
                            <asp:ListItem Text="Descending" Value="DESC" />
                        </asp:DropDownList>
                        <asp:PlaceHolder ID="phPageNavigation" runat="server">
                            <asp:Button ID="btnGoPage" runat="server" Text="Go to" CommandName="GoToPage" oncommand="ControlClick_Command" />
                            <asp:TextBox ID="txtGoPage" runat="server" Width="30px" MaxLength="3" />
                            <asp:Button ID="btnPrev" runat="server" CommandName="GoToPrev" Text="<<" oncommand="ControlClick_Command" />
                            <asp:Literal ID="ltPage" runat="server" />
                            <asp:Button ID="btnNext" runat="server" CommandName="GoToNext" Text=">>" oncommand="ControlClick_Command" />
                        </asp:PlaceHolder>
                        Records/Page: <asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true" onselectedindexchanged="ddlPageSize_SelectedIndexChanged" />
                    </td>
                </tr>
                <tr>
                    <td class="tdLeftT">
                        <table style="width:100%" class="tblGrid">
                            <tr>
                                <th class="thGrid" style="width:5%;">No. <input type="checkbox" id="chkAll" /></th>
                                <th class="thGrid" style="width:10%;">Avatar</th>
                                <th class="thGrid" style="width:20%;"><asp:LinkButton ID="lbProductNameSort" runat="server" Text="Product Name" CommandArgument="DESC" CommandName="proName" onclick="Sort_Click" CssClass="GridHeader sortAsc" /></th>
                                <th class="thGrid" style="width:10%;"><asp:LinkButton ID="lnOnHandQtySort" runat="server" Text="OnHand Qty" CommandArgument="DESC" CommandName="proOnHandQty" onclick="Sort_Click" CssClass="GridHeader" /></th>
                                <th class="thGrid" style="width:5%;"><asp:LinkButton ID="lbStatusSort" runat="server" Text="Active" CommandArgument="DESC" CommandName="proStatus" onclick="Sort_Click" CssClass="GridHeader" /></th>
                                <th class="thGrid" style="width:20%;">Categories</th>
                                <th class="thGrid" style="width:10%;"><asp:LinkButton ID="lbPriceSort" runat="server" Text="Price" CommandArgument="DESC" CommandName="proPrice" CssClass="GridHeader" onclick="Sort_Click" /></th>
                                <th class="thGrid" style="width:20%;">Comments</th>
                            </tr>
                            <tr>
                                <td class="tdGrid tdCenterM">
                                    <table class="centerObject">
                                        <tr>
                                            <td><asp:ImageButton ID="btnClearSearch" runat="server" ImageUrl="images/ico_clear_search.gif" 
                                        ToolTip="Clear Search" onclick="btnClearSearch_Click" /></td>
                                            <td><asp:ImageButton ID="btnSearch" runat="server" ImageUrl="images/ico_search.gif" ToolTip="Search" 
                                        onclick="btnSearch_Click" /></td>
                                        </tr>
                                    </table>
                                </td>
                                <td>&nbsp;</td>
                                <td class="tdGrid tdCenterM"><asp:TextBox ID="txtProductName" runat="server" Width="98%" /></td>
                                <td class="tdGrid tdCenterM">
                                    <asp:DropDownList ID="ddlCompareOnHand" runat="server" Width="45px" >
                                        <asp:ListItem Text="=" Value="=" />
                                        <asp:ListItem Text="<>" Value="<>" />
                                        <asp:ListItem Text=">" Value=">" />
                                        <asp:ListItem Text="<" Value="<" />
                                    </asp:DropDownList>
                                    <asp:TextBox ID="txtOnHandQty" runat="server" Width="40px" MaxLength="4" />
                                </td>
                                <td class="tdGrid tdCenterM">
                                    <asp:DropDownList ID="ddlStatus" runat="server">
                                        <asp:ListItem Text="All" Value="" />
                                        <asp:ListItem Text="Yes" Value="1" />
                                        <asp:ListItem Text="No" Value="0" />
                                    </asp:DropDownList>
                                </td>
                                <td class="tdGrid tdCenterM"><asp:TextBox ID="txtCategory" runat="server" Width="98%" /></td>
                                <td class="tdGrid tdCenterM">
                                    <asp:DropDownList ID="ddlComparePrice" runat="server" Width="45px" >
                                        <asp:ListItem Text="=" Value="=" />
                                        <asp:ListItem Text="<>" Value="<>" />
                                        <asp:ListItem Text=">" Value=">" />
                                        <asp:ListItem Text="<" Value="<" />
                                    </asp:DropDownList>
                                <asp:TextBox ID="txtPrice" runat="server" Width="40px" MaxLength="10" />
                                </td>
                                <td class="tdGrid tdCenterM"><asp:TextBox ID="txtProductDesc" runat="server" Width="98%" /></td>
                            </tr>
                            <asp:Repeater ID="rpProduct" runat="server">
                                <ItemTemplate>
                                    <tr>
                                        <td class="tdGrid tdLeftM"><%# Container.ItemIndex + 1 %> <input type="checkbox" id="chk_<%#Eval("ProductISN")%>" value="<%#Eval("ProductISN")%>" /></td>
                                        <td class="tdGrid tdCenterM"><div class="avatar"><%#GetProductAvatar(Eval("ProductISN"), Eval("Avatar"))%><%--<img src="<%# GetImageUrl(Eval("ProductISN"), Eval("Avatar")) %>" alt="" />--%></div></td>
                                        <td class="tdGrid tdLeftM">
                                            <div style="float:left">
                                                <a href="ProductDetails.aspx?isn=<%#Eval("ProductISN")%>"><%# ThuVien.FunctionsUtils.HTMLEncode(Eval("proName"))%></a>
                                            </div>
                                            <div style="float:right;text-align:right;"><img id="img<%#Eval("ProductISN")%>" alt="" src="images/ico_down.png" /></div>
                                        </td>
                                        <td class="tdGrid tdRightM"><%# Eval("proOnHandQty")%></td>
                                        <td class="tdGrid tdCenterM"><%#ThuVien.FunctionsUtils.GetStatus(Eval("proStatus"))%></td>
                                        <td class="tdGrid tdLeftM"><%#ThuVien.FunctionsUtils.HTMLEncode(Eval("Categorys")) %></td>
                                        <td class="tdGrid tdRightM"><%#ThuVien.FunctionsUtils.Format(Eval("proPrice"), ThuVien.FunctionsUtils.GetConfigValue("NumberFormat"))%></td>
                                        <td class="tdGrid tdLeftM"><%# ThuVien.FunctionsUtils.HTMLEncode(Eval("proComment")) %></td>
                                    </tr>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:Literal ID="ltNoRecord" runat="server" Visible="false">
                                <tr><td colspan="8" class="tdGrid tdCenterM">No Record</td></tr>
                            </asp:Literal>                            
                        </table>
                    </td>
                </tr>
            </table>
            <ul id="menufivelist" style="display:none;">
                <li>Product Info</li>
                <li>Pictures</li>
                <li>Specifications</li>
                <li></li>
                <li>Delete</li>
            </ul>
            <script type="text/javascript">
                function LoadJS() {              
                    requiredCheckBox("btnDelete", "chk_");
                    //check all
                    $("#chkAll").on("click", function() {
                        var checked_status = this.checked;
                        $('input[id^="chk_"]').each(function() {
                            this.checked = checked_status;
                            //$(this).parents('tr:first').removeClass('selected');
                            //if (this.checked)
                            //    $(this).parents('tr:first').addClass('selected');

                            requiredCheckBox("btnDelete", "chk_");
                        });
                    });
                    //check box
                    $('input[id^="chk_"]').on("click", function() {
                        console.log(this.checked);
                        checkOrUncheckForCheckAll();
                        requiredCheckBox("btnDelete", "chk_");
                    });
                    //add
                    $("#btnAdd").on("click", function(e) {
                        e.preventDefault();
                        OpenDialog("Add Product", "Product.aspx", 480, 500);
                    });
                    //$("ul[id^='mnu']").menu();

                    $('img[id^="img"]').each(function() {
                        var isn = $(this).attr("id").replace("img", "");
                        //console.log(isn);
                        ShowMenuInGrid(isn);
                    });
                }
                function requiredCheckBox(btnID, chkID) {
                    $('input[id$="' + btnID + '"]').attr('disabled', true);
                    var lstISN = [];

                    //for each tat ca checkbox checked
                    $('input[id^="' + chkID + '"]:checked').each(function() {
                        $('input[id$="' + btnID + '"]').attr('disabled', false);
                        var isn = this.id.replace(chkID, "");
                        lstISN.push(isn);
                    });

                    // set value va`o hidden field
                    $('input[id$="hdList"]').val(lstISN.join(","));
                }
                function checkOrUncheckForCheckAll() {
                    var checked = false;
                    if ($('input[id^="chk_"]:checkbox').length == $('input:checkbox[id^="chk_"]:checked').length) {
                        checked = true;
                    }
                    $('#chkAll').prop("checked", checked);
                }
                function DeleteConfirm(){
                    if ($('input:checkbox[id^="chk_"]:checked').length > 0) {
                        return confirm("Are you sure to delete?");
                    }
                    else {
                        return false;
                    }
                }
                function ShowMenuInGrid(isn) {
                    var opts = { minWidth: 120, hideDelay: 100, onClick: function(e, menuItem) { $.Menu.closeAll(); SelectMenu($(this).text(), isn); } };
                    $('img[id^="img'+ isn +'"]').menu(opts, '#menufivelist');
                }
                function SelectMenu(id, isn) {
                    switch (id) {
                        case "Product Info":
                            location.href = "ProductDetails.aspx?isn=" + isn;
                            break;
                        case "Pictures":
                            location.href = "ProductDetails.aspx?isn=" + isn + "&idx=1";
                            break;
                        case "Specifications":
                            location.href = "ProductDetails.aspx?isn=" + isn + "&idx=2";
                            break;
                        case "Delete":
                            if (confirm("Are you sure to delete?") == true) {
                                __doPostBack("<%=UpdatePanel.ClientID%>", 'del' + isn);
                            }
                            break;
                    }
                }
                function GetImageRealSize(id) {
                    var screenImage = $(id);
                    var theImage = new Image();
                    theImage.src = $(id).attr("src");
                    var imageWidth = theImage.width;
                    var imageHeight = theImage.height;
                    return { 'width': imageWidth, 'height': imageHeight };
                }
                function ResizeImage(id, objMaxSize, objRealSize){
                    if (objRealSize != null) {
                        id = '#' + id;
                        var ratio;
                        var width = objRealSize.width;
                        var height = objRealSize.height;

                        if (objRealSize.width > objRealSize.height) {
                            if (objRealSize.width > objMaxSize.width) {
                                ratio = objRealSize.width / objRealSize.height;
                                width = objMaxSize.width;
                                height = objMaxSize.width / ratio;
                            }
                        } else {
                            if (objRealSize.height > objMaxSize.height) {
                                ratio = objRealSize.height / objRealSize.width;
                                height = objMaxSize.height;
                                width = objMaxSize.height / ratio;
                            }
                        }

                        if (width > objMaxSize.width)
                            width = objMaxSize.width;
                        if (height > objMaxSize.height)
                            height = objMaxSize.height;

                        $(id).css("height", height);
                        $(id).css("width", width);
                    }
                }
            </script>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>